package com.ctgu.tengyun.controller.permission;

import com.ctgu.tengyun.bean.response.RolePermission;
import com.ctgu.tengyun.bean.response.UserPermissionListResponse;
import com.ctgu.tengyun.domain.PermissionResource;
import com.ctgu.tengyun.domain.SysRole;
import com.ctgu.tengyun.manager.PermissionManager;
import com.ctgu.tengyun.service.permission.PermissionResourceService;
import com.ctgu.tengyun.service.role.SysRoleService;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by liaoweizhi on 2019/8/16.
 */
@RestController
@RequestMapping(value = "/v1/permission/")
public class PermissionController {

    @Resource
    private PermissionManager permissionManager;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private PermissionResourceService permissionResourceService;

    @GetMapping(value = "all_resource")
    public List<PermissionResource> allResource() {
        List<PermissionResource> allResources = permissionResourceService.findAll();
        return allResources.stream().sorted(Comparator.comparing(PermissionResource::getSourceKey)).collect(Collectors.toList());
    }

    @GetMapping(value = "role_resource")
    public UserPermissionListResponse roleResource() {
        List<SysRole> roleList = sysRoleService.selectAll();

        UserPermissionListResponse response = new UserPermissionListResponse();
        List<RolePermission> rolePermissionList = new ArrayList<>();

        roleList.forEach(item -> {
            RolePermission rolePermission = new RolePermission();
            List<PermissionResource> permissionInfoList = permissionManager.getPermissionInfoListByRoleId(item.getId());
            rolePermission.setRoleId(item.getId());
            rolePermission.setRoleName(item.getName());
            rolePermission.setRolePermissionList(permissionInfoList);

            rolePermissionList.add(rolePermission);
        });

        response.setRolePermissionList(rolePermissionList);
        return response;
    }

    @GetMapping(value = "role_resource/{roleId}")
    public RolePermission roleResource(@PathVariable Integer roleId) {
        SysRole sysRole = sysRoleService.selectByPrimaryKey(roleId);
        Validator.assertResourceFound(null != sysRole, "role not found, roleId:" + roleId);

        List<PermissionResource> permissionInfoList = permissionManager.getPermissionInfoListByRoleId(sysRole.getId());

        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(sysRole.getId());
        rolePermission.setRoleName(sysRole.getName());
        rolePermission.setRolePermissionList(permissionInfoList);

        return rolePermission;
    }

    @PostMapping(value = "save_or_update")
    public Object saveOrUpdate(@RequestBody RolePermission request) {
        Validator.assertHasFields(null != request.getRoleId(), "roleId is required.");

        Integer result = permissionManager.upsert(request);

        return ImmutableMap.of("id", result);

    }

}
